package com.project.blog.controller;

import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;
import com.project.blog.common.result.Result;
import com.project.blog.common.result.ResultCode;
import com.project.blog.entity.Article;
import com.project.blog.entity.ArticleBody;
import com.project.blog.entity.ArticleQuery;
import com.project.blog.service.ArticleService;
import com.project.blog.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/list")
    // @RequiresAuthentication
    // @LogAnnotation(module = "文章", operation = "发布文章")
    public Result list(@RequestBody ArticleQuery query) {
        PageResult<Article> vo = articleService.list(query);
        Result r = Result.success();
        r.setData(vo);
        return r;
    }
    @PostMapping("/getArticlesByTag")
    // @RequiresAuthentication
    // @LogAnnotation(module = "文章", operation = "发布文章")
    public Result getArticlesByTag(@RequestBody ArticleQuery query) {
        PageResult<Article> vo = articleService.getArticlesByTag(query);
        Result r = Result.success();
        r.setData(vo);
        return r;
    }


    @PostMapping("/publish")
    // @RequiresAuthentication
    // @LogAnnotation(module = "文章", operation = "发布文章")
    public Result saveArticle(@Validated @RequestBody Article article) {
        Integer articleId = articleService.publishArticle(article);
        Result r = Result.success();
        r.simple().put("articleId", articleId);
        return r;
    }

    @GetMapping("/new")
    @FastJsonView(include = {@FastJsonFilter(clazz = Article.class, props = {"id", "title", "createDate"})})
    //  @LogAnnotation(module = "文章", operation = "获取最新文章")
    public Result listNewArticles() {
        List<Article> articles = articleService.listNewArticles();
        return Result.success(articles);
    }


    @GetMapping("/view/{id}")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = Article.class, props = {"comments"}),
                    @FastJsonFilter(clazz = ArticleBody.class, props = {"contentHtml"}),
                    // @FastJsonFilter(clazz = Tag.class, props = {"avatar"})
            }
            // include = {@FastJsonFilter(clazz = User.class, props = {"id", "nickname", "avatar"})}
    )
    //@LogAnnotation(module = "文章", operation = "根据id获取文章，添加阅读数")
    public Result getArticleAndAddViews(@PathVariable("id") Integer id) {
        Result r = new Result();
        if (null == id) {
            r.setResultCode(ResultCode.PARAM_IS_BLANK);
            return r;
        }
        Article article = articleService.getArticleAndAddViews(id);
        r.setResultCode(ResultCode.SUCCESS);
        r.setData(article);
        return r;
    }

}
